package com.mobartisan.vehiclenetstore.getui_push;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.mobartisan.vehiclenetstore.R;
import com.mobartisan.vehiclenetstore.util.AndroidUtils;
import com.mobartisan.vehiclenetstore.util.ObjectUtils;



public class NotificitionManager {
    private static final String TAG = NotificitionManager.class.getSimpleName();

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static void showNotification(Context context, int notifyCode, String contentTitle, String contentText, String packageName, Bundle bundle) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();
        // 定义下拉通知栏时要展现的内容信息
        Intent notificationIntent = new Intent();
        try {
            String packageNames = "com.mobartisan.workinglunch";
            if (AndroidUtils.isRunningApp(context, packageName)) {
                Log.d(TAG, "----isRunningApp--->packageNames: " + packageNames);
                notificationIntent.setClass(context, Class.forName(packageNames + ".ui.activitiy.WelcomeActivity"));
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            } else if (!ObjectUtils.isNullOrEmpty(packageName)) {
                Log.d(TAG, "---packageName---->packageName: " + packageNames);
                notificationIntent.setClass(context, Class.forName(packageName));
            }
            if (!ObjectUtils.isObjectEmpty(bundle)) {
                notificationIntent.putExtra("data", bundle);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Bitmap bitMap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.logo)).getBitmap();
        // 用mNotificationManager的notify方法通知用户生成标题栏消息通知
        // .setVibrate(new long[]{100,250,100,500}) 设置震动
        Notification notification = new Notification.Builder(context)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setLargeIcon(bitMap)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(contentTitle)
                .setWhen(when)
                // .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        notification.icon = R.mipmap.logo;
        notificationManager.notify(notifyCode, notification);
    }

    public static void showNofi(Context context, String contentTitle, String contentText, Class reciver, Bundle bundle) {
        Intent broadcastIntent = new Intent(context, reciver);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Bitmap bitMap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.logo)).getBitmap();
        // 用mNotificationManager的notify方法通知用户生成标题栏消息通知
        long when = System.currentTimeMillis();
        builder
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setLargeIcon(bitMap)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(contentTitle)
                .setWhen(when)
                //.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(2, builder.build());
    }


    public static void showNofi(Context context, String url, String contentTitle, String contentText, Class reciver) {
        url = "https://m.baidu.com/";
        Intent broadcastIntent = new Intent(context, reciver);
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        broadcastIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.
                getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Bitmap bitMap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.logo)).getBitmap();
        // 用mNotificationManager的notify方法通知用户生成标题栏消息通知
        long when = System.currentTimeMillis();
        builder
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setLargeIcon(bitMap)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(contentTitle)
                .setWhen(when)
//                .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.cat))
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(2, builder.build());
    }

    /**
     *
     * @param context
     * @param contentTitle
     * @param contentText
     */
    public static void showNofi(Context context, String contentTitle, String contentText,int notiCode) {
        Intent broadcastIntent =  context.getPackageManager().getLaunchIntentForPackage("com.nari.cattle");
        broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Bitmap bitMap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.logo)).getBitmap();
        // 用mNotificationManager的notify方法通知用户生成标题栏消息通知
        long when = System.currentTimeMillis();
        builder
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setLargeIcon(bitMap)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(contentTitle)
                .setWhen(when)
                //.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
//                .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + com.narixt.models.R.raw.cat))
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notiCode, builder.build());
    }


}
